﻿using System;
using Microsoft.Popfly.GameCreator.GameEngine.Behaviors;

namespace Microsoft.Popfly.GameCreator.GameEngine.Events
{
    public class DistanceEventChecker : IEventChecker
    {
        private Game m_game;
        private BehaviorOwner m_owner;
        private MotionBehavior m_behavior;
        private double m_distance;
        private bool m_continueFiring;
        internal double RotationDistanceFromStart { get; set; }
        internal double StartingRotationValue { get; set; }
        internal double LastRotationValue { get; set; }
        internal double StartingX { get; set; }
        internal double StartingY { get; set; }

        public DistanceEventChecker(Game game, BehaviorOwner owner, MotionBehavior behavior, double distance, bool continueFiring)
        {
            m_game = game;
            m_owner = owner;
            m_behavior = behavior;
            m_distance = distance;
            m_continueFiring = continueFiring;
            RotationDistanceFromStart = 0;
            StartingX = owner.X;
            StartingY = owner.Y;
            if (owner.IsActor)
            {
                Actor actorOwner = (Actor)owner;
                StartingRotationValue = actorOwner.Rotation;
                LastRotationValue = actorOwner.Rotation;
            }
        }

        #region IEventChecker Members

        public bool FireThisFrame(double elapsedTimeInSeconds, out BehaviorOwner sender, out PGCEventArgs args)
        {
            if (m_behavior.ReachedDistanceFromStart(this, m_distance))
            {
                sender = m_owner.Scene;
                args = null;

                if (!m_continueFiring)
                {
                    m_owner.RemoveBehavior(this);
                }
                RotationDistanceFromStart = 0;
                if (m_owner.IsActor)
                {
                    Actor actorOwner = (Actor)m_owner;
                    StartingRotationValue = actorOwner.Rotation;
                    LastRotationValue = actorOwner.Rotation;
                }
                StartingX = m_owner.X;
                StartingY = m_owner.Y;
                return true;
            }
            else
            {
                sender = null;
                args = null;

                return false;
            }
        }

        // not a custom code created event (custom code uses every frame checkers)
        public bool IsOfEventType(string eventType)
        {
            return false;
        }


        #endregion
    }
}
